package com.zhiyou100.test.test04.demo;

import java.lang.reflect.Proxy;

/**
 * @author yang
 * @className SimpleDynamicProxy
 * @packageName com.zhiyou100.test.test04.demo
 * @Description TODO 动态代理 demo
 * @date 2020-08-11 11:52
 */
public class SimpleDynamicProxy {
    public static void consumer(Interface iface){
        iface.doSomething();
        iface.somethingsElse("bonobo");
    }

    public static void main(String[] args) {
        final RealObject realObject = new RealObject();

        consumer(realObject);

        final Object o = Proxy.newProxyInstance(
                Interface.class.getClassLoader(),
                new Class[]{Interface.class},
                new DynamicProxyHandler(realObject)
        );
        final Interface anInterface = (Interface) (o);
        // proxy: class com.sun.proxy.$Proxy0

        consumer(anInterface);
    }
}
